Skip to content

Sockets - Cyndi#11

Open
cyndilopez wants to merge 1 commit intoAda-C11:masterfrom
cyndilopez:master
Open

Sockets - Cyndi#11
cyndilopez wants to merge 1 commit intoAda-C11:masterfrom
cyndilopez:master

Conversation

@cyndilopez
Copy link
Copy Markdown

No description provided.

Copy link
Copy Markdown

@eric-andeen eric-andeen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code's fine, but by creating a hash, you're not really solving the problem as described. Try again to solve it without any additional allocations beyond the output array.

Comment thread lib/array_intersection.rb
# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n+m) ~= O(n) where n is the length of the larger array because the code iterates through each item in both of the arrays. Looking up items in a hash table is O(1) time complexity; therefore the linear time complexity is dependent on the size of the larger array.
# Space complexity: O(n) - a new array is created to store intersecting values so space complexity is (worst-case) dependent on the length, n, of the array
Copy link
Copy Markdown

@eric-andeen eric-andeen Apr 19, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space complexity is O(n) for your solution - you're essentially making a whole copy of array1 in the hash. The assignment specifically says you should minimize space complexity. Can you do better than O(n)? Hint - the answer is yes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants